package com.jpl.seminar.ui.module.menu;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.jpl.seminar.ui.SeminarManagerMobileApplication;
import com.jpl.seminar.ui.module.info.InfosView;
import com.jpl.seminar.ui.module.registration.RegistrationView;
import com.vaadin.addon.touchkit.ui.NavigationBar;
import com.vaadin.addon.touchkit.ui.NavigationButton;
import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.addon.touchkit.ui.TouchKitApplication;
import com.vaadin.addon.touchkit.ui.VerticalComponentGroup;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

/**
 * Displays jpl main menu options
 */
public class MenuView extends NavigationView {

	private static final long serialVersionUID = 1L;

	private Resource menuIcon = new ThemeResource("../runo/icons/64/globe.png");
	static Resource refreshIcon = new ThemeResource("graphics/reload-icon.png");

	public MenuView(final MenuManager nav) {

		setCaption("JPL Seminar Registration");
		setWidth("100%");
		setHeight("100%");

		CssLayout root = new CssLayout();
		root.setWidth("100%");
		root.setMargin(true);

		VerticalComponentGroup accounts = new VerticalComponentGroup();
		Label header = new Label("Options");
		header.setSizeUndefined();
		header.addStyleName("grey-title");
		root.addComponent(header);

		NavigationButton infosButton = new NavigationButton("Infos");
		infosButton.addStyleName("pill");
		infosButton.setIcon(menuIcon);
		accounts.addComponent(infosButton);

		infosButton.addListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				InfosView v = new InfosView(nav);
				nav.navigateTo(v);
			}
		});

		NavigationButton registrationButton = new NavigationButton("Registration");
		registrationButton.addStyleName("pill");
		registrationButton.setIcon(menuIcon);
		registrationButton.addListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				RegistrationView v = new RegistrationView(nav);
				nav.navigateTo(v);
			}
		});

		accounts.addComponent(registrationButton);

		root.addComponent(accounts);
		setContent(root);
		setToolbar(createToolbar());
	}

	public static Component createToolbar() {

		final NavigationBar toolbar = new NavigationBar();

		Button refresh = new Button();
		refresh.setIcon(refreshIcon);
		refresh.addStyleName("no-decoration");

		toolbar.setLeftComponent(refresh);

		final SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
		toolbar.setCaption("Updated " + formatter.format(Calendar.getInstance().getTime()));

		refresh.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				toolbar.setCaption("Updated " + formatter.format(Calendar.getInstance().getTime()));
			}
		});

		TouchKitApplication touchKitApplication = SeminarManagerMobileApplication.get();
		if (touchKitApplication instanceof SeminarManagerMobileApplication) {
			SeminarManagerMobileApplication app = (SeminarManagerMobileApplication) touchKitApplication;
			if (app.isSmallScreenDevice()) {
				/*
				 * For small screen devices we add shortcut to new message below
				 * hierarcy views
				 */
				ClickListener showComposeview = new ClickListener() {
					public void buttonClick(ClickEvent event) {
						Window window = event.getButton().getWindow();
						window.showNotification("not implemented.");
					}
				};
				Button button = new Button(null, showComposeview);
				button.setIcon(new ThemeResource("graphics/compose-icon.png"));
				toolbar.setRightComponent(button);
				button.addStyleName("no-decoration");
			}
		}

		return toolbar;
	}
}
